package cn.sytton.taffe.auth.service.security;

import cn.sytton.taffe.auth.entity.AuthUserEntity;
import cn.sytton.taffe.auth.mapper.SecurityMapper;
import cn.sytton.taffe.framework.security.entity.LoginUser;
import cn.sytton.taffe.framework.security.pwd.IUserPwdDetailsService;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 用户密码UserDetailsService
 *
 * @author skyrock
 */
@Component
public class UserPwdDetailsService implements IUserPwdDetailsService {

    @Resource
    private SecurityMapper securityMapper;

    @Resource
    private UserDetailFactory userDetailFactory;

    @Override
    public LoginUser loadUserByUsername(String loginName) throws UsernameNotFoundException {
        AuthUserEntity user = securityMapper.findUserByLoginName(loginName);
        if (user == null) {
            throw new BadCredentialsException("账号或密码错误");
        }
        return userDetailFactory.create(loginName, user);
    }
}
